//
//  M_FavCell.swift
//  letuofu
//
//  Created by WH-JS on 2019/12/27.
//  Copyright © 2019 WH-JS. All rights reserved.
//

import UIKit

class M_FavCell: UITableViewCell {
    //定义一个闭包类型
    typealias moreBlock = ([String : Any?]) -> Void
    //声明一个变量
    var moreBtnBack: moreBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func callmoreBlock(block: @escaping moreBlock) {
        moreBtnBack = block
    }
    
    //定义一个闭包类型
    typealias delBlock = (UITableViewCell) -> Void
    //声明一个变量
    var delBtnBack: delBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func calldelBlock(block: @escaping delBlock) {
        delBtnBack = block
    }
    
    let bgView = UIView()
    let productImg = UIImageView()
    let priceImg = UIImageView()
    let moreBtn = Button()
    let titleLab = UILabel()
    let priceLab = UILabel()
    let subLab = UILabel()
    let delButton = Button()
    var isedit = false
    
    var celldata : [String : Any?]? {
        didSet{
            loadData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setupUI() {
        self.backgroundColor = .white
        drawView(view: bgView) { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.width.equalTo(118)
            make.height.equalTo(118)
        }
        bgView.drawImage(imageView: productImg,named: nil) { (make) in
            make.width.equalToSuperview().offset(-12)
            make.height.equalToSuperview().offset(-12)
            make.center.equalToSuperview()
        }
        drawLabel(label: titleLab, text: "", fontSize: 12, fontWeight: .medium, textColor: .x333333) { (make) in
            make.left.equalTo(bgView.snp.right).offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(15)
        }
        titleLab.numberOfLines = 0
        
        //画横线
        let line = drawView { (make) in
            make.left.equalToSuperview().offset(140.5)
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
//        drawImage(imageView: priceImg,named: "jifen_13") { (make) in
//            make.left.equalTo(bgView.snp.right).offset(10)
//            make.bottom.equalToSuperview().offset(-16)
//        }
        drawLabel(label: priceLab, text: priceLab.text, fontSize: 18, fontWeight: .regular, textColor: .xFC4C02) { (make) in
//            make.left.equalTo(priceImg.snp.right).offset(5)
            
            make.left.equalTo(bgView.snp.right).offset(10)
            make.bottom.equalToSuperview().offset(-15)
        }
        drawLabel(label: subLab, text: subLab.text, fontSize: 11, fontWeight: .regular, textColor: .x999999) { (make) in
            make.left.equalTo(priceLab.snp.right).offset(10)
            make.bottom.equalToSuperview().offset(-17)
        }
        drawButton(button: moreBtn,imageNamed: "more3") { (make) in
            make.right.equalToSuperview().offset(-10.5)
            make.bottom.equalToSuperview().offset(-11)
        }
        moreBtn.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
        moreBtn.addTarget(self, action: #selector(_addButtonClick(button:)), for: .touchUpInside)
        
        drawButton(button: delButton,title: "删除", titleColor: .white ,fontSize: 16) { (make) in
            make.left.equalTo(moreBtn.snp_right).offset(10.5)
            make.width.equalTo(80)
            make.top.bottom.equalToSuperview()
        }
        delButton.backgroundColor = .xFC4C02
        delButton.addTarget(self, action: #selector(_delButtonClick(button:)), for: .touchUpInside)
    }
    
    func loadData() {
        if var urlstring = celldata?.getString("productURL") {
            if celldata?.getString("zoneCode") == "105002" {
                urlstring = (JDIMG_HOST_URL+urlstring).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            }
            else {
                urlstring = (IMG_HOST_URL+urlstring).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            }
            let url = URL(string: urlstring)
            productImg.sd_setImage(with:url)
        }else{
            productImg.image = nil
        }
        titleLab.text = celldata?.getString("productName")
        subLab.text = celldata?.getString("centUnitName")
        priceLab.text = anyToString(celldata?["employeeCentPrice"] ?? "")?.quXiaoShuDianHouDeLing

        self.center.x = self.frame.size.width/2
        isedit = false
    }
    
    @objc func _addButtonClick(button: Button) {
        if moreBtnBack != nil {
            moreBtnBack!(celldata ?? [:])
        }
        UIView.animate(withDuration: 0.4) {
            if self.isedit {
                self.center.x += 80
            }
            else {
                self.center.x -= 80
            }
            self.isedit = !self.isedit
        }
    }
    
    
    @objc func _delButtonClick(button: Button) {
        if delBtnBack != nil {
            delBtnBack!(self)
        }
    }
    
    //
    func ermakeUIEdit() {
        bgView.snp_remakeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(50)
            make.width.equalTo(118)
            make.height.equalTo(118)
        }
    }
    
    //
    func ermakeUINomal() {
        bgView.snp_remakeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.width.equalTo(118)
            make.height.equalTo(118)
        }
    }
    
    //View外点击
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        if view == nil {
            for subview in self.subviews {
                let subPoint = subview.convert(point, from: self)
                if subview.bounds.contains(subPoint) {
                    return subview
                }
            }
        }
        return view
    }
}
